package com.rmc.verifycode.service.web;


import com.rmc.http.accesser.HttpAccesser;
import com.rmc.http.accesser.HttpException;
import com.rmc.verifycode.service.parser.AbstractVerifyCodeParser;

import java.io.File;

/**
 * User: Administrator
 */
public class WebServiceVerifyCodeParser extends AbstractVerifyCodeParser {
	private String id;
	private final String webServiceUrl;

	public WebServiceVerifyCodeParser(String webServiceUrl) {
		this.webServiceUrl = webServiceUrl;
	}

	@Override
	protected String parseCode(File image, HttpAccesser accesser) {
		if (id == null) {
			id = image.getName();
		}
//		showCodeImage(imageFile);
		if (image == null) {
			return "";
		}
		try {
			return accesser.buildReq(webServiceUrl)
			               .param("id", id).fileParam("image", image)
			               .submit();
		} catch (HttpException e) {
			e.printStackTrace();
			return "";
		}
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "WebServiceVerifyCodeParser{" +
				"id='" + id + '\'' +
				", webServiceUrl='" + webServiceUrl + '\'' +
				'}';
	}
}
